package com.tiger.admin.http.common.dictionary;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tiger.admin.Db;
import com.tiger.admin.SaveOperateLog;
import com.tiger.admin.http.common.dictionary.dto.Add;
import com.tiger.admin.http.common.dictionary.dto.Update;
import com.tiger.db.entity.DictionaryCategory;
import com.tiger.db.entity.DictionaryItem;
import com.tiger.utils.ResponseUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/dictionary/category")
public class Category {
    private final Db db;

    public Category(Db db) {
        this.db = db;
    }



    @GetMapping("items")
    public Object items() {
        List<DictionaryCategory> categorys = db.dictionaryCategory.list();
        List<DictionaryItem> items = db.dictionaryItem.list();

        Map<String, Object> res = new HashMap<>();
        res.put("categorys", categorys);
        res.put("items", items);

        return ResponseUtil.successRes(res);
    }

    @GetMapping("list")
    public Object list() {
        List<DictionaryCategory> list = db.dictionaryCategory.list();

        return ResponseUtil.successRes(list);
    }

    @SaveOperateLog("新增字典分类")
    @PostMapping("add")
    public Object add(@RequestBody @Validated Add data) {
        if(db.dictionaryCategory.exists(new QueryWrapper<DictionaryCategory>().eq("name", data.getName()))) {
            return ResponseUtil.failRes("已存在相同的名称");
        }

        if(db.dictionaryCategory.exists(new QueryWrapper<DictionaryCategory>().eq("code", data.getCode()))) {
            return ResponseUtil.failRes("已存在相同的编码的");
        }

        DictionaryCategory category = new DictionaryCategory();
        category.setName(data.getName());
        category.setCode(data.getCode());

        boolean r = db.dictionaryCategory.save(category);

        return r ? ResponseUtil.successRes(category) : ResponseUtil.failRes();
    }

    @SaveOperateLog("更新字典分类")
    @PostMapping("update/{id}")
    public Object add(@RequestBody @Validated Update data, @PathVariable long id) {
        DictionaryCategory row = db.dictionaryCategory.getOne(new QueryWrapper<DictionaryCategory>().select("name", "code").eq("id", id));
        if(row == null) return ResponseUtil.failRes();



        if(!Objects.equals(data.getName(), row.getName())) {
            if(db.dictionaryCategory.exists(new QueryWrapper<DictionaryCategory>().eq("name", data.getName()))) {
                return ResponseUtil.failRes("已存在相同名称");
            }
        }

        if(!Objects.equals(data.getCode(), row.getCode())) {
            if(db.dictionaryCategory.exists(new QueryWrapper<DictionaryCategory>().eq("code", data.getCode()))) {
                return ResponseUtil.failRes("已存在相同编码");
            }
        }

        DictionaryCategory updateData = new DictionaryCategory();
        updateData.setName(data.getName());
        updateData.setCode(data.getCode());
        updateData.setId(id);

        boolean r = db.dictionaryCategory.updateById(updateData);

        return r ? ResponseUtil.successRes(updateData) : ResponseUtil.failRes();
    }

    @SaveOperateLog("删除字典分类")
    @PostMapping("delete/{id}")
    public Object del(@PathVariable long id) {
        DictionaryItem item = db.dictionaryItem.getOne(new QueryWrapper<DictionaryItem>().select("id").eq("category_id", id));
        System.out.println(item);

        if(item != null) {
            return ResponseUtil.failRes("该分类下存在字典");
        }

        boolean r = db.dictionaryCategory.removeById(id);
        return r ? ResponseUtil.successRes() : ResponseUtil.failRes();
    }
}
